defmodule SockWorkstation.CoopHelperSup do
  use DynamicSupervisor

  alias SockWorkstation.{
    # SockCoopClient,
    SockTTSClient,
    SockASRClient,
    # GrpcLLMClient,
    # GrpcVADClient,
    # CoopMgrGen,
    # CodecNifGen,
    # OpusDecoderGen,
    # PcmEncoderGen,
    # FrameHelperGen,
    SockVOTTSClient,
    SockVOASRClient
  }

  @name __MODULE__

  def start_link(args) do
    DynamicSupervisor.start_link(@name, args, name: @name)
  end

  @impl true
  def init(_args) do
    DynamicSupervisor.init(strategy: :one_for_one)
  end

  def super_asr_client(session_id) do
    # 生成唯一的 ID
    unique_id = make_ref()

    spec = %{
      id: {:worker, unique_id},
      start: {SockASRClient, :start_link, [session_id]},
      restart: :temporary
    }

    DynamicSupervisor.start_child(__MODULE__, spec)
  end

  def super_voasr_client(session_id) do
    # 生成唯一的 ID
    unique_id = make_ref()

    spec = %{
      id: {:worker, unique_id},
      start: {SockVOASRClient, :start_link, [session_id]},
      restart: :temporary
    }

    DynamicSupervisor.start_child(__MODULE__, spec)
  end

  def super_tts_client({session_id, text}) do
    # 生成唯一的 ID
    unique_id = make_ref()

    spec = %{
      id: {:worker, unique_id},
      start: {SockTTSClient, :start_link, [{session_id, text}]},
      restart: :temporary
    }

    DynamicSupervisor.start_child(__MODULE__, spec)
  end

  def super_votts_client({sess_id, text}) do
    # 生成唯一的 ID
    unique_id = make_ref()

    spec = %{
      id: {:worker, unique_id},
      start: {SockVOTTSClient, :start_link, [{sess_id, text}]},
      restart: :temporary
    }

    DynamicSupervisor.start_child(__MODULE__, spec)
  end
end

# def super_coop_mgr(session_id) do
#   # 生成唯一的 ID
#   unique_id = make_ref()

#   spec = %{
#     id: {:worker, unique_id},
#     start: {CoopMgrGen, :start_link, [session_id]},
#     restart: :temporary
#   }

#   case DynamicSupervisor.start_child(__MODULE__, spec) do
#     {:ok, pid} ->
#       {:ok, pid}

#     {:error, {:already_started, pid}} ->
#       # coop_mgr = CoopMgrGen.update_state(session_id, pid)
#       # is_pid(coop_mgr.coop_pid) || send(coop_mgr.coop_pid, {:run_add_coop, coop_mgr.coop_pid})
#       # DynamicSupervisor.terminate_child(__MODULE__, pid)
#       # DynamicSupervisor.start_child(__MODULE__, spec)
#       {:ok, pid}

#     {:error, _} ->
#       IO.puts("start coop mgr failed")
#   end

#   # DynamicSupervisor.start_child(__MODULE__, spec)
# end

# def super_asr_helper(session_id) do
#   # 生成唯一的 ID
#   unique_id = make_ref()

#   spec = %{
#     id: {:worker, unique_id},
#     start: {SockWorkstation.ASRHelperGen, :start_link, [session_id]},
#     restart: :temporary
#   }

#   # DynamicSupervisor.start_child(__MODULE__, spec)
#   case DynamicSupervisor.start_child(__MODULE__, spec) do
#     {:ok, pid} ->
#       {:ok, pid}

#     {:error, {:already_started, pid}} ->
#       {:ok, pid}

#     {:error, _} ->
#       IO.puts("start asr helper failed")
#   end
# end

# def super_tts_helper(session_id) do
#   # 生成唯一的 ID
#   unique_id = make_ref()

#   spec = %{
#     id: {:worker, unique_id},
#     start: {SockWorkstation.TTSHelperGen, :start_link, [session_id]},
#     restart: :temporary
#   }

#   # DynamicSupervisor.start_child(__MODULE__, spec)
#   case DynamicSupervisor.start_child(__MODULE__, spec) do
#     {:ok, pid} ->
#       {:ok, pid}

#     {:error, {:already_started, pid}} ->
#       {:ok, pid}

#     {:error, _} ->
#       IO.puts("start tts helper failed")
#   end
# end

# def super_coop_client(session_id) do
#   # 生成唯一的 ID
#   unique_id = make_ref()

#   spec = %{
#     id: {:worker, unique_id},
#     start: {SockCoopClient, :start_link, [session_id]},
#     restart: :temporary
#   }

#   # DynamicSupervisor.start_child(__MODULE__, spec)
#   case DynamicSupervisor.start_child(__MODULE__, spec) do
#     {:ok, pid} ->
#       {:ok, pid}

#     {:error, {:already_started, pid}} ->
#       # DynamicSupervisor.terminate_child(__MODULE__, pid)
#       # DynamicSupervisor.start_child(__MODULE__, spec)
#       {:ok, pid}

#     {:error, _} ->
#       IO.puts("start coop client failed")
#   end
# end

# def super_llm_client(session_id) do
#   # 生成唯一的 ID
#   unique_id = make_ref()

#   spec = %{
#     id: {:worker, unique_id},
#     start: {GrpcLLMClient, :start_link, [session_id]},
#     restart: :temporary
#   }

#   # DynamicSupervisor.start_child(__MODULE__, spec)
#   case DynamicSupervisor.start_child(__MODULE__, spec) do
#     {:ok, pid} ->
#       {:ok, pid}

#     {:error, {:already_started, pid}} ->
#       # DynamicSupervisor.terminate_child(__MODULE__, pid)
#       # DynamicSupervisor.start_child(__MODULE__, spec)
#       # CoopMgrGen.add_to_pcm_pid(session_id, pid)
#       {:ok, pid}

#     {:error, _} ->
#       IO.puts("start llm client failed")
#   end
# end

# def super_vad_client(session_id) do
#   # 生成唯一的 ID
#   unique_id = make_ref()

#   spec = %{
#     id: {:worker, unique_id},
#     start: {GrpcVADClient, :start_link, [session_id]},
#     restart: :temporary
#   }

#   # DynamicSupervisor.start_child(__MODULE__, spec)
#   case DynamicSupervisor.start_child(__MODULE__, spec) do
#     {:ok, pid} ->
#       {:ok, pid}

#     {:error, {:already_started, pid}} ->
#       # CoopMgrGen.add_to_pcm_pid(session_id, pid)
#       {:ok, pid}

#     {:error, _} ->
#       IO.puts("start vad client failed")
#   end
# end

# def super_frame_queue(session_id) do
#   # 生成唯一的 ID
#   unique_id = make_ref()

#   spec = %{
#     id: {:worker, unique_id},
#     start: {FrameHelperGen, :start_link, [session_id]},
#     restart: :temporary
#   }

#   DynamicSupervisor.start_child(__MODULE__, spec)
# end

# def super_opus_decode({session_id, pid}) do
#   # 生成唯一的 ID
#   unique_id = make_ref()

#   spec = %{
#     id: {:worker, unique_id},
#     start: {OpusDecoderGen, :start_link, [{session_id, pid}]},
#     restart: :temporary
#   }

#   DynamicSupervisor.start_child(__MODULE__, spec)
# end

# def super_pcm_encode(session_id) do
#   # 生成唯一的 ID
#   unique_id = make_ref()

#   spec = %{
#     id: {:worker, unique_id},
#     start: {PcmEncoderGen, :start_link, [session_id]},
#     restart: :temporary
#   }

#   DynamicSupervisor.start_child(__MODULE__, spec)
#   # case DynamicSupervisor.start_child(__MODULE__, spec) do
#   #   {:ok, pid} ->
#   #     {:ok, pid}

#   #   {:error, {:already_started, pid}} ->
#   #     CoopMgrGen.add_to_opus_pid(session_id, pid)
#   #     {:ok, pid}

#   #   {:error, _} ->
#   #     IO.puts("start pcm encoder failed")
#   # end
# end

# def super_nif_codec(session_id) do
#   # 生成唯一的 ID
#   unique_id = make_ref()

#   spec = %{id: {:worker, unique_id}, start: {CodecNifGen, :start_link, [session_id]}}
#   DynamicSupervisor.start_child(__MODULE__, spec)
# end

#   defp via_tuple(session_id, type) do
#   {:via, Registry, {SockWorkstation.SessionRegistry, {session_id, type}}}
# end
# end
